var evaluationGoodsId = ''
$(document).ready(function(){
    $('.switchBtn').css('border','1px solid #b4b4b4')
    $('.switchBtn').children('.round').css('background','#b4b4b4')
    $('.switchBtn').eq(0).css('border','1px solid #0b67ff')
    $('.switchBtn').eq(0).children('.round').css('background','#0b67ff')
    getEvaluationList(evaluationPageNo,showNum2,evaluationGoodsId)
});


var t = 0
$(".switchBtn").on('click',function(){
    var type = $(this).data('type')
    t = type
    $('.switchBtn').css('border','1px solid #b4b4b4')
    $('.switchBtn').children('.round').css('background','#b4b4b4')
    $(this).css('border','1px solid #0b67ff')
    $(this).children('.round').css('background','#0b67ff')
    getEvaluationList(evaluationPageNo,showNum2,evaluationGoodsId)
})

$(".dateSearch").on('click',function(){
    getEvaluationList(evaluationPageNo,showNum2,evaluationGoodsId)
})

var evaluationPageNo = 1,showNum2=10;
var evaluationList = '';
function getEvaluationList(p,showNum2,id){
    let time = '00:00:00'
    let endTime = '23:59:59'
    var start = $('#start').val() != ''? $('#start').val() + ' ' + time: '2019-01-01' + ' ' + time;//开始时间
    var end = $('#end').val() != ''? $('#end').val() + ' ' + endTime: laydate.now(0, 'YYYY-MM-DD') + ' ' + endTime;//结束时间
    start = Date.parse(start) / 1000
    end = Date.parse(end) / 1000
    // console.log(start)
    // console.log(end)
    // console.log(id)
    // console.log(start)
    
    getAjax(`${global.apiUrl}/Api/Admincommunityhealthshop/get_community_health_shop_tell`,function(res){
        console.log(res);
        if(res.list.length == 0){
            $("#page").paging({
                pageNo:0,
                totalPage: 0, //总页数
                totalSize: 0,
                callback: function(num) {
                    getEvaluationList(num,showNum2,id)
                }
            });
            var html = `
                    <div>
                        暂无评论
                    </div>`
            $('#evaluateMainAre').html(html);
        }else{
            $("#page").paging({
                pageNo:p,
                totalPage: Math.ceil(res.count/showNum2), //总页数
                totalSize: res.count,
                callback: function(num) {
                    getEvaluationList(num,showNum2,id)
                    evaluationPageNo = num
                }
            });
            var html = '';
            evaluationList = res.list;
            var list = res.list;
            for(var i in list){
                html += `
                        <div class="evaluateAre">
                            <div class="evaluateItemAre">
                                <div class="evaluateItemTitle">
                                    <div class="userAre">
                                        <img src="${list[i].user_head_img}" style="width: 35px;height: 35px;margin-right: 15px;border-radius:  50%"/>
                                        <div style="color: black;font-size: 16px;margin-right: 15px;">${list[i].user_name}</div>
                                        <div>客户评论</div>
                                    </div>
                                    <div>${list[i].date}</div>
                                </div>
                                <div class="commentContent">${list[i].content}</div>
                                <div class="evaluateImgAre">
                                    ${getEvaluateImg(list[i].content_img)}
                                </div>
                                ${getReplay(list[i].hf,list[i].id)}
                                
                            </div>
                        </div>
            `
            }
            $('#evaluateMainAre').html(html);
        }
    },{
        p:p,
        num:showNum2,
        product_id: id,
        status: t,
        start_time: start,
        end_time: end,
        shop_id: global.shop_id
    })

    
}

function getEvaluateImg(list){
    var img = ''
    if(list.length != 0){
        var img = ''
        for(var i in list){
            img += `<img src="${global.apiUrl +  list[i]}"></img> `
        }
        
    }
    return img
}

function getReplay(list,id){
    console.log(list)
    var html = ''
    if(list != null){
        html += `
                <div class="replyAre">
                    <div class="triangle"></div>
                    <div>商家回复：</div>
                    <div style="margin-top: 10px">${list.content}</div>
                </div>    
                `
    }else{
        html += `<div>
                    <div class="hf1" style="display: block">
                        <div class="replyAre2">
                            <div class="replyBtn" data-id="${id}">
                                <img src="../../img/message.png">
                                <div>回复</div>
                            </div>
                        </div>
                    </div>
                    
                    <div class="hf2" style="display: none">
                        <div class="replyItemAre" >
                            <textarea class="replyInput" maxlength="-1" placeholder="请输入对此评论的回复" placeholder-style="" auto-focus auto-height fixed adjust-position show-confirm-bar bindconfirm="replyOk"></textarea>
                            <div class="replyItemTxtAre" >
                                <div style="margin-right:15px;" class="cancel">取消</div>
                                <div class="confirm" data-id="${id}">确认</div>
                            </div>
                        </div>
                    </div>
                    
                   
                </div>`
    }
    return html
}

$('#evaluateMainAre').on('click','.replyBtn',function(){
    var id = $(this).data('id');
    $(this).parent().parent().css('display','none')
    $(this).parent().parent().next().css('display','block')
})

$('#evaluateMainAre').on('click','.cancel',function(){
    var index = $(this).data('index');
    $(this).parent().prev().val('')
    console.log($(this).parent().parent().parent().prev())
    $(this).parent().parent().parent().prev().css('display','block')
    $(this).parent().parent().parent().css('display','none')
})

$('#evaluateMainAre').on('click','.confirm',function(){
    var id = $(this).data('id');
    var index = $(this).data('index');
    var content = $(this).parent().prev().val()
    getAjax(`${global.apiUrl}/Api/Admincommunityhealthshop/tell`,function(res){
        console.log(res)
        if(res == 1){
          //成功 
          alert('回复成功')
          getEvaluationList(evaluationPageNo,showNum2,evaluationGoodsId)
        }else{
          alert('回复失败，请重试')
        }
      },{fid: id,content: content})
})